﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artech.PetShop.Common;
using System.Transactions;
using Artech.PetShop.Orders.BusinessEntity;
using System.Data;

namespace Artech.PetShop.Orders.DataAccess
{
  public  class OrderDA: DataAccessBase
    {
      public void Submit(Order order)
      {
          order.OrderNo = Guid.NewGuid();
          string procedureName = "P_ORDER_INSERT";
          Dictionary<string, object> parameters = new Dictionary<string, object>();
          parameters.Add("p_order_id",      order.OrderNo.ToString());
          parameters.Add("p_ordered_by",    ApplicationContext.Current.UserName);
          parameters.Add("p_total_price",   order.TotalPrice);
          parameters.Add("p_user_name",     ApplicationContext.Current.UserName);
          parameters.Add("p_transacion_id", Transaction.Current.TransactionInformation.LocalIdentifier);
          this.Helper.ExecuteNonQuery(procedureName, parameters);

          procedureName = "P_ORDER_DETAIL_INSERT";
          foreach (OrderDetail detail in order.Details)
          {
              parameters.Clear();
              parameters.Add("p_order_id",      order.OrderNo.ToString());
              parameters.Add("p_product_id",    detail.ProductID.ToString());
              parameters.Add("p_quantity",      detail.Quantity);
              parameters.Add("p_user_name",     ApplicationContext.Current.UserName);
              parameters.Add("p_transacion_id", Transaction.Current.TransactionInformation.LocalIdentifier);
              this.Helper.ExecuteNonQuery(procedureName, parameters);
          }
      }
    }
}
